<?php
namespace Functions\Model;
use Think\Model;
/**
 * 服务类型操作
 */
class ServiceModel extends Model {

    //完成
    protected $_auto = array (

    );

    //验证
    protected $_validate = array(
        array('service_name','require', '请填写服务类型名称'),
        array('service_sort','require', '请填写服务类型排序'),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0){
        return  $this->where($where)->limit($limit)->select();
    }

    /**
     * 获取列表
     * @return array 列表
     */
    public function countList($where = array()){
        return  $this->where($where)->count();
    }
    /**
     * 获取信息
     * @param int $serviceId ID
     * @return array 信息
     */
    public function getInfo($serviceId)
    {
    	return $this->where('service_id ='.$serviceId)->find();
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {

    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        
        //事务处理
        $this->startTrans();
        $data = $this->create();
        if(!$data){
            $this->rollback();
            return false;
        }
        if($type == 'add'){
            //写入数据
            $status = $this->add();
            if($status){
                $this->commit();
            }else{
                $this->rollback();
            }
            return $status;
        }
        if($type == 'edit'){
            //修改数据
            $status = $this->where('service_id='.$data['service_id'])->save();
            if($status === false){
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        }
        $this->rollback();
        return false;
    }

    /**
     * 删除信息
     * @param int $serviceId ID
     * @return bool 删除状态
     */
    public function delData($serviceId)
    {
        $map = array();
        $map['service_id'] = $serviceId;
        return $this->where($map)->delete();
    }

    /**
     *获取菜单模块参数
     */
    public function getModuleMenu(){
        $serviceList = $this->loadList();
        $i=0;
        foreach ($serviceList as $_k => $_v) {
            $menu[$i]['name'] = $_v['service_name'];
            $menu[$i]['url']  = U('Appoint/index',array('service_id'=>$_v['service_id']));
            $menu[$i]['icon'] = '';
            $i++;
        }
        return $menu;
    }

}
